package com.timmy.google_play.holder.detail;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.timmy.google_play.R;
import com.timmy.google_play.base.BaseHolder;
import com.timmy.google_play.bean.ItemBean;
import com.timmy.google_play.utils.UIUtils;

import butterknife.BindView;

/**
 * Created by Timmy on 2016/12/14.
 */
public class DetailDesHolder extends BaseHolder<ItemBean> implements View.OnClickListener {
    @BindView(R.id.app_detail_des_tv_des)
    TextView mAppDetailDesTvDes;

    @BindView(R.id.app_detail_des_tv_author)
    TextView mAppDetailDesTvAuthor;

    @BindView(R.id.app_detail_des_iv_arrow)
    ImageView mAppDetailDesIvArrow;
    private ItemBean mData;

    boolean isOpen = true;
    private int mAppDetailDesTvDesMeasuredHeight;

    @Override
    protected View initHolderVIew() {

        View view = View.inflate(UIUtils.getContext(), R.layout.item_detail_des, null);
        view.setOnClickListener(this);
        return view;
    }

    @Override
    protected void refreshHolderView(ItemBean data) {
        mData = data;

        mAppDetailDesTvDes.setText(data.des);
        mAppDetailDesTvAuthor.setText(data.author);

        mAppDetailDesTvDes.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

            }
        });

    }

    @Override
    public void onClick(View view) {
        changeAppDatailDesTvDesHeight(true);
    }

    private void changeAppDatailDesTvDesHeight(boolean isAnimation) {

        if (isOpen) {           //折叠

            if (mAppDetailDesTvDesMeasuredHeight == 0) {
                mAppDetailDesTvDesMeasuredHeight = mAppDetailDesTvDes.getMeasuredHeight();
            }

            int start = mAppDetailDesTvDesMeasuredHeight;
            int end   = getLineHeight(7, mData.des);

            if (isAnimation) {
                doAnimation(start, end);
            } else {
                mAppDetailDesTvDes.setHeight(end);
            }

        } else {        //展开

            int start = getLineHeight(7, mData.des);

            int end = mAppDetailDesTvDesMeasuredHeight;

            if (isAnimation) {
                doAnimation(start, end);
            } else {
                mAppDetailDesTvDes.setHeight(end);
            }
        }

        //取反
        isOpen = !isOpen;
    }

    //动画效果
    private void doAnimation(int start, int end) {

        ObjectAnimator animator = ObjectAnimator.ofInt(mAppDetailDesTvDes, "height", start, end);

        animator.start();

        if (isOpen) {
            ObjectAnimator.ofFloat(mAppDetailDesIvArrow, "rotation", 180, 0).start();
        } else {
            ObjectAnimator.ofFloat(mAppDetailDesIvArrow, "rotation", 0, 180).start();
        }

        //监听动画执行效果,找到外层 scrollView ,然后进行滚动
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                // 循环遍历找到外层 scrollView ,然后进行滚动
                ViewParent parent = mAppDetailDesTvDes.getParent();

                while (true) {
                    parent = parent.getParent();

                    if (parent instanceof ScrollView) {
                        //找到
                        ((ScrollView) parent).fullScroll(View.FOCUS_DOWN);
                        break;
                    }
                }
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

    }

    //页面详情,显示多少行
    private int getLineHeight(int lineHeight, String des) {


        TextView tv = new TextView(UIUtils.getContext());

        tv.setText(des);
        tv.setLines(lineHeight);

        tv.measure(0, 0);
        int measuredHeight = tv.getMeasuredHeight();

        return measuredHeight;
    }
}
